<template>
	<view class="maintenance">
		<toSafe :bgColor="'#fff'"></toSafe>
		<view class="searchInput">
			<view class="search-top">
				<view class="icon">
					<uni-icons type="left" size="30" @click="HandleReturn" style="margin-right: 20rpx;"></uni-icons>
				</view>
				<view class="title">火灾自动报警系统</view>
			</view>
			<button class="top-btn" @click="seeDirectory">查看目录</button>
		</view>
		<up-loadmore status="loading" v-if="isLoading" />
		<scroll-view :scroll-y="true" class="updata-form" v-else>
			<up-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png"
				v-if="!form.wbPlanClassifyYearContentThreeVos || form.wbPlanClassifyYearContentThreeVos?.length == 0">
			</up-empty>
			<view class="control-room" v-for="item in form.wbPlanClassifyYearContentThreeVos"
				:key="item.classifyContentId">
				<text class="control-title">{{item.name}}</text>
				<text class="control-message">{{item.standardRequest??'暂无简介'}}</text>
				<view class="control-info">
					<view class="position">
						<view class="label">
							<text>检查部位</text>
							<text>具体位置精确到楼层</text>
						</view>
						<up-input v-model="item.classifyName" placeholder="请输入" clearable border='none'
							customStyle="background-color:#F6F8FA;height: 64rpx;"></up-input>
					</view>
					<view class="message">
						<view class="label">检查情况及处理说明</view>
						<up-textarea v-model="item.actualMeasurement" placeholder="请输入"
							customStyle="background-color:#F6F8FA;"></up-textarea>
					</view>
					<view class="result">
						<view class="label">
							结果判定
						</view>
						<!-- <up-input v-model="item.isOk" placeholder="请输入合格或不合格" clearable border='none'
							customStyle="background-color:#F6F8FA;">
						</up-input> -->
						<up-radio-group v-model="item.isOk" placement="row">
							<up-radio :customStyle="{marginBottom: '8px'}" label="无" name=" "
								style="margin-right: 30rpx;">
							</up-radio>
							
							<up-radio :customStyle="{marginBottom: '8px'}" label="合格" name="合格"
								style="margin-right: 30rpx;">
							</up-radio>
							<up-radio :customStyle="{marginBottom: '8px'}" label="不合格" name="不合格">
							</up-radio>
						</up-radio-group>
					</view>
				</view>
			</view>
		</scroll-view>
		<!-- 底部按钮 -->
		<view class="fire-buttom">
			<view class="up-page" @click="upPage">
				<!-- <up-icon size="40" name="@/static/image/上一页.png"></up-icon> -->
				<image src="@/static/image/uppage.png" mode=""></image>
				<text>上一页</text>
			</view>
			<view class="down-page" @click="downPage">
				<!-- <up-icon size="40" name="@/static/image/下一页.png"></up-icon> -->
				<image src="@/static/image/downpage.png" mode=""></image>
				<text>下一页</text>
			</view>
			<button class="submit" @click="submit">提交</button>
			<!-- <button class="submit" @click="check" v-else>审核</button> -->
			<!-- <button type="button" @click="previewHandler">预览</button> -->
			<!-- <button class="load-receipt" @click="loadReceipt">上传凭条</button> -->
			<!-- <button class="load-receipt" @click="loadReceipt" v-else>查看凭条</button> -->
		</view>
		<!-- 预览 -->
		<!-- <view class="float-preview">
			<button type="button" @click="previewHandler">预览</button>
		</view> -->
		<up-popup :show="isShow" :closeOnClickOverlay="false" :round="10" mode="bottom">
			<view class="popup">
				<view class="popup-top">
					<view class="top-left" @click="coles">
						取消
					</view>
					<view class="top-right" @click="popupSubmit">
						确认
					</view>
				</view>
				<view class="popup-body">
					<uni-forms ref="valiForm" :rules="rules" :modelValue="checkForm" :label-width="80"
						label-align="right">
						<uni-forms-item label="是否合格" name="test">
							<uni-data-checkbox v-model="checkForm.test" :localdata="tests" style="line-height: 100%;" />
						</uni-forms-item>
						<uni-forms-item label="描述" name="introduction">
							<uni-easyinput type="textarea" v-model="checkForm.auditRemark" placeholder="请输入审核结果描述" />
						</uni-forms-item>
					</uni-forms>
				</view>
			</view>
		</up-popup>
	</view>
</template>

<script setup>
	import {
		getCatalogueYearList,
		planAudit,
		checkReject,
		submitYearForm,
		getAllList,
		saveCatalog,
		saveCatalogNew,
		allListByCreateBy,
		batchEdit
	} from "@/api/maintenance.js"
	import toSafe from '@/compoents/top-safe.vue'
	import EventBus from '@/utils/eventBus.js'
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app'
	import {
		ref,
		reactive,
		onMounted,
		onUpdated,
	} from 'vue'
	let form = reactive({})
	let dataLists = reactive([])
	const isLoading = ref(false)
	let itemId = ref()
	let isId = ref()
	let isRoles = ref(true)
	let isShow = ref(false)
	let checkForm = reactive({
		test: 0,
		auditRemark: '',
	})
	let tests = reactive([{
		text: '合格',
		value: 0
	}, {
		text: '不合格',
		value: 1
	}])
	let rules = reactive({})
	let pageList = reactive([])
	const nameValue = ref(1)
	// 建筑信息列表
	const tableData = reactive([{
		text: "A", //显示
		value: "" // 值
	}])

	onLoad(options => {
		itemId.value = options.itemId
		isId.value = options.id
		nameValue.value = options.nameValue
		getRoles()
		allListByCreate()
	})
	// onShow(() => {
	// 	getList()
	// 	getPage()
	// })
	const getRoles = () => {
		let roles = uni.getStorageSync('roles')
		roles.split(',').forEach(item => {
			if (item == '一级消防师' || item == '工程师') {
				isRoles.value = false
			} else {
				isRoles.value = true
			}
		})
	}
	//查询有没有新增
	const allListByCreate = () => {
		isLoading.value = true
		allListByCreateBy().then(res => {
			// console.log(res.rows);
			if (!res.rows || res.rows.length == 0) {
				getList()
			} else {
				const list = []
				res.rows.forEach(item => {
					console.log(item, 888);
					item.wbPlanClassifyYearContentVos?.forEach(i => {
						i.wbPlanClassifyYearContentThreeVos?.forEach(el => {
							el.name = i.classifyName
							el.isOk = 3
						})
					})
					list.push(...item.wbPlanClassifyYearContentVos)
				})
				dataLists.splice(0, dataLists.length, ...list)
				pageList.splice(0, dataLists.length, ...res.rows)
				getPage(isId.value)
				isLoading.value = false
			}
		})

	}
	//
	const saveCatalogNewFun = (rows) => {
		saveCatalogNew(rows).then(res => {
			if (res.code == 200) {
				allListByCreate()
			}
		})

	}
	//获取模板
	const getList = () => {
		isLoading.value = true
		getAllList().then(res => {
			if (res.code == 200) {
				saveCatalogNewFun(res.rows)
			}
			isLoading.value = false
		}).catch(err => {
			isLoading.value = false
		})
	}
	const getPage = (id = null) => {
		if (id) {
			dataLists?.forEach(item => {
				if (item.classifyYearContentId == id) {
					getForm(item)
				}
			})
		} else {
			getForm(dataLists[0])
		}
	}
	// 判断是否合格
	const getForm = item => {
		// const item = JSON.parse(uni.getStorageSync('form_Item'))
		item?.wbPlanClassifyYearContentThreeVos?.map(i => {
			// i.isOk = i.result == 1 ? '合格' : '不合格'
			if (i.result == 1) {
				i.isOk ='合格'
			}
			
			if (i.result == 2) {
				i.isOk ='不合格'
			}
			if (i.result == 3) {
				i.isOk ='无'
			}
		})
		console.log(item, 'form');
		Object.assign(form, item)
	}
	// 上一页
	const upPage = () => {
		dataLists.forEach((item, index) => {
			if (form.classifyYearContentId !== item.classifyYearContentId) return
			if (index == 0) {
				uni.showToast({
					title: '已经是第一页了！',
					icon: 'none',
				})
			} else {
				Object.assign(form, dataLists[index - 1])
			}
		})
		// EventBus.emit('updata-page', form.index - 1)
		// getForm()
	}

	// 下一页
	const downPage = () => {
		const matchingItem = dataLists.find(item => item.classifyYearContentId === form.classifyYearContentId);
		if (matchingItem) {
			const nextIndex = dataLists.indexOf(matchingItem) + 1;
			if (nextIndex < dataLists.length) {
				Object.assign(form, dataLists[nextIndex]);
			} else {
				uni.showToast({
					title: '已经是最后一页了！',
					icon: 'none',
				});
			}
		}
		// EventBus.emit('updata-page', form.index + 1)
		// getForm()
	}

	const HandleReturn = () => {
		// uni.navigateBack({
		// 	delta: 1
		// });
		uni.switchTab({
			url:'/pages/index/index'
		})
	}

	// 查看目录
	const seeDirectory = () => {
		uni.navigateTo({
			url: `/pages/engineering/classMy?nameValue=${nameValue.value || 1}`
		})
	}

	// 提交任务
	const submit = () => {
		// form.contentList?.forEach(item => {
		// 	item.planId = itemId.value
		// })
		// const updatedArray = pageList.map(obj => {
		//     if (obj.hasOwnProperty('wbPlanClassifyYearContentThreeVos')) {
		//         const newObj = { ...obj };
		//         delete newObj.wbPlanClassifyYearContentThreeVos;
		//         newObj.wbPlanClassifyYearContentThreeVos = obj.wbPlanClassifyYearContentThreeVos;
		//         return newObj;
		//     }
		//     return obj;
		// });
		// console.log(updatedArray,'updatedArray');
		// saveCatalog(updatedArray).then(res => {
		// 	if (res.code == 200) {
		// 		uni.showToast({
		// 			title: '任务提交成功！'
		// 		})
		// 	}
		// })
		uni.showLoading({
			title: '任务提交中'
		})
		let data = []
		dataLists.forEach(item => {
			item.wbPlanClassifyYearContentThreeVos.forEach(i => {
				data.push(i)
			})
		})
		console.log(data);
		batchEdit(data).then(res => {
			if (res.code == 200) {
				uni.showToast({
					title: '任务提交成功！'
				})
			}
		})
	}
	// 上传凭条
	const loadReceipt = () => {
		uni.navigateTo({
			url: `/pages/engineering/updata?id=${itemId.value}`
		})
	}

	// 审核
	const check = () => {
		isShow.value = true
	}
	const popupSubmit = () => {
		let form = {
			planId: itemId.value
		}
		Object.assign(form, checkForm)
		if (checkForm.test == 0) {
			planAudit(form).then(res => {
				if (res.code == 200) {
					uni.showToast({
						title: '审核成功',
						icon: 'none'
					});
					Object.assign(checkForm, {
						test: 0,
						auditRemark: '',
					})
				}
			})
		} else {
			checkReject(form).then(res => {
				if (res.code == 200) {
					uni.showToast({
						title: '审核成功',
						icon: 'none'
					});
					Object.assign(checkForm, {
						test: 0,
						auditRemark: '',
					})
				}
			})
		}
	}
	const coles = () => {
		isShow.value = false
		Object.assign(checkForm, {
			test: 0,
			auditRemark: '',
		})
	}

	// 预览
	const previewHandler = () => {
		uni.navigateTo({
			url: `/pages/proview/index?type=${'年检'}&id=${itemId.value}`
		})
	}
</script>

<style lang="less" scoped>
	@import "../../App.less";

	.maintenance {
		width: 100%;
		height: 100%;
		background: #F6F8FA;
		overflow: hidden;

		.searchInput {
			width: 100%;
			height: 88rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			background-color: #fff;

			// position: fixed;
			// top: 0;
			// left: 0;
			padding: 0 20rpx;
			z-index: 99;

			.search-top {
				.inline_center();
				height: 88rpx;
				width: 100%;
				// display: flex;
				// align-items: center;

				.icon {
					width: 10vw;
					height: 88rpx;
					line-height: 88rpx;
					z-index: 1;
				}

				.title {
					width: 90vw;
					line-height: 88rpx;
					height: 88rpx;
					text-align: center;
					margin-left: -8vw;
					font-size: 34rpx;
					font-weight: 700;
				}

			}

			.top-btn {
				position: absolute;
				right: 28rpx;
				// width: 160rpx;
				height: 64rpx;
				line-height: 64rpx;
				background-color: #325EF5;
				color: #fff;
				font-size: 28rpx
			}

			.search_icons {
				.inline_center();
				width: 100%;
				height: 68rpx;
				padding: 16rpx 20rpx;
				background-color: #F2F2F2;
				border-radius: 34rpx;

				.icon {
					margin-right: 10rpx;
				}

				.uni-input {
					width: 100%;
					border-right: 1px solid #325EF5;
					margin-right: 20rpx;
					height: 100%;
				}

				.search {
					background-color: inherit;
					width: 100rpx;
					height: 68rpx;
					font-size: 32rpx;
					color: #325EF5;
					line-height: 68rpx;
				}
			}

		}

		.updata-form {
			padding: 0 28rpx;
			height: calc(100vh - 200rpx);

			.control-room {
				display: flex;
				flex-direction: column;

				.control-title {
					font-size: 28rpx;
					font-weight: bold;
				}

				.control-message {
					padding: 10rpx 0;
					font-size: 24rpx;
					opacity: 0.6;
				}

				.control-info {
					padding: 20rpx;
					background-color: #fff;
					border-radius: 12rpx;

					.position {
						.label {
							margin-bottom: 10rpx;

							text:nth-child(2) {
								font-size: 24rpx;
								opacity: 0.6;
								margin-left: 20rpx;
							}
						}
					}

					.message {
						margin-top: 20rpx;

						.label {
							font-size: 28rpx;
							margin-bottom: 10rpx;
						}
					}

					.result {
						margin-top: 20rpx;

						.label {
							font-size: 28rpx;
							margin-bottom: 10rpx;
						}

						.result-check {
							display: flex;

							>view {
								flex: 1;
							}
						}
					}
				}

				&:nth-child(1) {
					margin-top: 20rpx;
				}

				&::after {
					content: '';
					display: block;
					width: 100%;
					height: 20rpx;
				}
			}


		}

		.fire-buttom {
			width: 100%;
			height: 112rpx;
			position: absolute;
			bottom: 0;
			padding: 9rpx 28rpx;
			background-color: #fff;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.up-page {
				display: flex;
				flex-direction: column;
				align-items: center;
				font-size: 24rpx;

				>image {
					width: 40rpx;
					height: 40rpx;
				}
			}

			.down-page {
				display: flex;
				flex-direction: column;
				align-items: center;
				font-size: 24rpx;

				>image {
					width: 40rpx;
					height: 40rpx;
				}
			}

			>button {
				width: 50%;
				height: 58rpx;
				background-color: #325EF5;
				color: #fff;
				border-radius: 16rpx;
				margin: 0;
				line-height: 58rpx;
				font-size: 26rpx;
			}
		}


		.search-placeholder {
			width: 100%;
			height: 60rpx;
			background-color: #cfd9f9;
			font-size: 28rpx;
			color: #325EF5;
			text-align: center;
			line-height: 60rpx;
		}


		.uni-list {
			height: 80rpx;
		}
	}

	.uni-group {
		display: flex;
		align-items: center;
	}

	.popup {
		height: 50vh;
		display: flex;
		flex-direction: column;

		.popup-top {
			padding: 10rpx 28rpx;
			font-size: 40rpx;
			color: #325EF5;
			.inline_space_between()
		}

		.popup-body {
			flex: 1;
			padding: 0 28rpx;
			// background-color: #dadada;
		}
	}

	.float-preview {
		position: fixed;
		right: 20rpx;
		bottom: 150rpx;

		button {
			border-radius: 10%;
			background-color: #325EF5;
			color: #fff;
		}
	}
</style>